package com.ccl.iot.monitor;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

public class NetworkMonitor extends Monitor {
	/*
	 * 	Static constants
	 */
    public static final int WIFI_STATE_DISABLING = WifiManager.WIFI_STATE_DISABLING;
    public static final int WIFI_STATE_DISABLED = WifiManager.WIFI_STATE_DISABLED;
    public static final int WIFI_STATE_ENABLING = WifiManager.WIFI_STATE_ENABLING;
    public static final int WIFI_STATE_ENABLED = WifiManager.WIFI_STATE_ENABLED;
    public static final int WIFI_STATE_UNKNOWN = WifiManager.WIFI_STATE_UNKNOWN;
    
    /*
     * 	Interfaces
     */
	public interface OnNetwrokStateChangeListener extends MonitorListener{
		public void onNetworkStateChange(NetworkMonitor aMonitor, boolean aConnected);
	}
	
	/*
	 * 	Static functions
	 */
	private static NetworkMonitor gDefault = null;
	
	public static void Initialize(Context aContext){
		if (gDefault == null){
			gDefault = new NetworkMonitor(aContext);
		}
	}
	
	public static boolean InstanceOf(Monitor aMonitor){
		return aMonitor == gDefault;
	}
	
	public static boolean IsConnected(){
		if (gDefault != null){
			return gDefault.isConnected();
		}
		
		return false;
	}
	
	public static boolean IsConnectedByMobile(){
		if (gDefault != null){
			return gDefault.isConnectedByMobile();
		}
		
		return false;
	}
	
	public static boolean AddOnNetworkStateChangeListener(OnNetwrokStateChangeListener aListener){
		if (gDefault != null){
			return gDefault.addListener(aListener);
		}
		
		return false;
	}
	
	public static boolean RemoveOnNetworkStateChangeListener(OnNetwrokStateChangeListener aListener){
		if (gDefault != null){
			return gDefault.removeListener(aListener);
		}
		
		return false;
	}
	
	/*
	 * 	Non-static functions
	 */
	private Context iContext = null;
	private ConnectivityManager iConnMan = null;
	
	private NetworkMonitor(Context aContext){
		iContext = aContext;
		
		if (aContext != null){
			iConnMan = (ConnectivityManager) aContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		}
	}
	
	public boolean isConnected(){
		if (iConnMan != null){
			NetworkInfo tInfo = iConnMan.getActiveNetworkInfo();
			
			return tInfo != null && tInfo.isConnected();
		}else{
			return false;
		}
	}
	
	public boolean isConnectedByMobile(){
		if (iConnMan != null){
			NetworkInfo tInfo = iConnMan.getActiveNetworkInfo();
			
			return tInfo != null && tInfo.isConnected() && tInfo.getType() == ConnectivityManager.TYPE_MOBILE;
		}else{
			return false;
		}
	}
	
	private BroadcastReceiver iNetworkInfoReceiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent != null){
				//if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
//					boolean tConnected = (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true) == false);
					boolean tConnected = isConnected();
					int tIndex = 0;
					OnNetwrokStateChangeListener tListener;
					
					tListener = (OnNetwrokStateChangeListener) getFirstListener();
					while(tListener != null){
						tListener.onNetworkStateChange(NetworkMonitor.this, tConnected);
						tListener = (OnNetwrokStateChangeListener) getNextListener(tIndex++);
					}
				//}
			}
		}					
	};
	
	@Override
	public boolean startMonitor(){
		if (iContext != null){
			if (super.startMonitor()){
				IntentFilter tFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
				
				iContext.registerReceiver(iNetworkInfoReceiver, tFilter);
				
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public boolean stopMonitor(boolean aForce){
		if (super.stopMonitor(aForce)){
			if (iContext != null){
				iContext.unregisterReceiver(iNetworkInfoReceiver);
			}
			
			return true;
		}
		
		return false;
	}
}
